﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Validation;
using Backend.JYD.Common;
using Backend.JYD.Model.Auth;

namespace Backend.JYD.EFDAL.DAL
{
    public class AuthorityInfoDAL : IDAL.Auth.IAuthorityInfo
    {
        public long AddAuthorityInfo(AuthorityInfo info)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    db.AuthorityInfoes.Add(info);
                    db.SaveChanges();
                    return info.AuthId;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return 0;
                }
            }
        }

        public bool UpdateAuthorityInfo(AuthorityInfo info)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    db.Entry<AuthorityInfo>(info).State = System.Data.Entity.EntityState.Modified;
                    return db.SaveChanges() > 0;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }

        public bool DeleteAuthorityInfo(long authId)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    var info = db.AuthorityInfoes.Find(authId);
                    db.Entry<AuthorityInfo>(info).State = System.Data.Entity.EntityState.Deleted;
                    return db.SaveChanges() > 0;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }

        public AuthorityInfo GetAuthorityInfoById(long authId)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    return db.AuthorityInfoes.Find(authId);
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }

        public IList<AuthorityInfo> GetAuthorityInfoByIds(IList<long> authIds)
        {
            Int64[] ids = authIds.ToArray();
            using (var db = new JydDataContext())
            {
                try
                {
                    var query = db.AuthorityInfoes.Where(p => ids.Contains(p.AuthId));
                    return query.ToList();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }
        public IList<AuthorityInfo> GetAllAuthorityInfo()
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    return db.AuthorityInfoes.ToList();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }
    }
}
